package com.health.ts.controller.patientview;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.patientview.StDocument;
import com.health.ts.service.patientview.StDocumentService;
/**
 * <p>
 *  患者一体化视图文书接口
 * </p>
 *
 * @author ${ts}
 * @since 2017-09-16
 */
@Controller
@RequestMapping(value = "/api/patientView",produces= BaseController.JSON_UTF8)
public class PatientViewDocumentController extends BaseController{
	
	@Autowired
	StDocumentService  documentService;
	
	/**
	 * 获取文书列表
	 * @param patientId
	 * @return
	 */
	@RequestMapping(value="/documents/{patientId}", method=RequestMethod.GET)
	@ResponseBody
	public ApiResult getDocumentList(@PathVariable String patientId,String documentType) {
		List<StDocument> documents = documentService.getDocuments(patientId,documentType);
		return ApiResult.success(documents);
	}
	
	/**
	 * 根据文书id获取文书文字内容
	 * @param patientId
	 * @return
	 */
	@RequestMapping(value="/document/{documentId}/text", method=RequestMethod.GET)
	@ResponseBody
	public ApiResult getDocumentText(@PathVariable String documentId) {
		String documentText = documentService.getDocumentText(documentId,"UTF-8");
		return ApiResult.success(documentText);
	}
	
	/**
	 * 根据文书id获取文书文件流
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/document/{documentId}/stream", method=RequestMethod.GET)
	public ResponseEntity<byte[]> getDocumentStream(@PathVariable String documentId) throws Exception {
		Map<String,Object> document=documentService.getDocumentBytes(documentId);
		String fileName=document.get("fileName").toString();
		byte[] docBytes =(byte[]) document.get("bytes");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(docBytes, headers, HttpStatus.OK);
	}
	
	/**
	 * 根据文书id获取pdf类型文书
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/document/{documentId}/pdf", method=RequestMethod.GET,produces="application/pdf")
	@ResponseBody
	public Object getDocumentPdf(@PathVariable String documentId) throws Exception {
		Map<String,Object> document=documentService.getDocumentBytes(documentId);
		byte[] docBytes =(byte[]) document.get("bytes");
		return  docBytes;
	}
}
